using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Config;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Base.Helper;
using Framework.Library.Data.DAL;
using System.Web.Configuration;
using System.Configuration;

namespace Framework.Portal.Layouts.Admin.Components.Portals
{
    public partial class PortalSettings : BaseControl
    {
        public string MENUNAME = string.Empty;

        public PortalSettings()
        {
        }

        public PortalSettings(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCommandName.Text = MENUNAME;
                var cfgHelper = new ConfigHelper();
                dgrPortals.DataSource = cfgHelper.lstApp;
                dgrPortals.DataBind();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Configuration myWebAppConfig;
                AppSettingsSection myAppSettings;

                myWebAppConfig = WebConfigurationManager.OpenWebConfiguration("~");
                myAppSettings = myWebAppConfig.AppSettings;
                //myAppSettings.Settings.Add(txtAppSettingKey.Text, txtAppSettingValue.Text);
                myAppSettings.Settings[txtAppSettingKey.Text].Value = txtAppSettingValue.Text;
                myWebAppConfig.Save();
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
        }

        protected void dgrPortals_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int portalId = ConvertUtility.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "editportal":
                    break;
                case "deleteportal":
                    break;
                default:
                    break;
            }
        }

        protected void dgrPortals_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                var btnDeleteRole = (ImageButton) e.Item.FindControl("btnDeletePortal");
                var lblDescription = (Label) e.Item.FindControl("lblDescription");
                var lblValue = (Label)e.Item.FindControl("lblValue");
                var kvp = (PortalSetting)e.Item.DataItem;
                
                lblValue.Text = kvp.VALUE;
                if (kvp.VALUE.Length > 30)
                    lblValue.Text = ConvertUtility.ToSubString(kvp.VALUE, 30);
                lblDescription.Text = kvp.DESCRIPTION;
                btnDeleteRole.OnClientClick = Localization.GetText("MESSAGE", "MSG_DELETE_CONFIRM");
            }
        }
    }
}